<template>
  <section class="component add-cost">
    <el-form :rules="rules" :model="model" ref="add-cost" label-width="90px">
      <el-form-item label="费用类别" prop="costType">
        <el-select v-model="model.costType" placeholder="请选择" clearable>
          <el-option v-for="{index,name,code} in $dict.getDictData('CostType')" :key="index" :label="name" :value="code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="费用项" prop="costEvent">
        <el-select v-model="model.costEvent" placeholder="请选择" clearable>
          <el-option v-for="{index,name,code} in $dict.getDictData('CostEvent')" :key="index" :label="name" :value="code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="费用金额" prop="momey">
        <el-input type="number" v-model="model.momey"></el-input>
      </el-form-item>
      <el-form-item label="缴费时间" prop="payTime">
        <el-date-picker v-model="model.payTime" type="date" placeholder="选择日期" :value-format="'yyyy-MM-dd'">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="付款凭证">
        <file-upload :fileList.sync="fileList"></file-upload>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="model.remark" :maxlength="200" show-word-limit></el-input>
      </el-form-item>
    </el-form>

    <div class="operate-buttons">
      <el-button @click="commit">确定</el-button>
        <el-button @click="close">取消</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { OutsourceLawsuitService } from "~/services/domain-service/outsource-lawsuit-manage-index.service";
import { Emit, Prop, Watch } from "vue-property-decorator";
import FileUpload from "~/components/common/file-upload.vue";
import { Getter } from "vuex-class";
import { FileUploadService } from "~/services/file-service/file-upload.service";

@Component({
  components: {
    FileUpload
  }
})
export default class AddCost extends Vue {
  @Dependencies(OutsourceLawsuitService) 
  private outsourceLawsuitService: OutsourceLawsuitService;
  @Dependencies(FileUploadService)
  private fileUploadService: FileUploadService;

  @Prop()
  private rowData
  @Prop()
  private rowCostData

  private fileList: any[] = []

  private model: any = {
    costType: "",
    costEvent: "",
    momey: "",
    payTime: "",
    remark: "",
  }

  private rules: any = {
    costType: [
      { required: true, message: '请选择费用类别', trigger: 'blur' }
    ],
    costEvent: [
      { required: true, message: '请选择费用项', trigger: 'blur' }
    ],
    momey: [
      { required: true, message: '请输入费用金额', trigger: 'blur' }
    ],
    payTime: [
      { required: true, message: '请选择缴费时间', trigger: 'blur' }
    ],
    remark: [
      { required: true, message: '请输入备注', trigger: 'blur' }
    ],
  };

  @Emit("refreshCostData")
  private refreshCostData() { }
  @Emit("close")
  private close() {
    const form : any = this.$refs['add-cost']
    form.resetFields()
    this.fileList = []
  }

  private commit() {
    const form: any = this.$refs['add-cost'];
    form.validate(valid => {
      if (!valid) return
      if (this.fileList.length === 0) {
        this.$message.error('请上传凭证')
        return
      }
      const fileIds = this.fileList.map(val => val.response.id).join(",")
      const id = this.rowCostData ? this.rowCostData.id : ''
      this.outsourceLawsuitService.saveOrUpdateCostRecord({...this.model, fileIds, assistId: this.rowData.id, id}).subscribe(
        data => {
          this.$message.success("操作成功");
          this.refreshCostData();
          this.close();
        }
      );
    });
  }

  private refresh() {
    if (!this.rowCostData) {
      this.$nextTick(() => {
        const form : any = this.$refs['add-cost']
        form.resetFields()
      })
      this.fileList = []
      return
    }
    this.model.costType = this.rowCostData.costType
    this.model.costEvent = this.rowCostData.costEvent
    this.model.momey = this.rowCostData.momey
    this.model.payTime = this.rowCostData.payTime
    this.model.remark= this.rowCostData.remark
    this.callbackFileList(this.rowCostData.fileIds)
  }

  private callbackFileList(fileIds: string) {
    if (!fileIds) return 
    this.fileUploadService.getAllUploadFileByIds(fileIds).subscribe(
      data => {
        if (!data || data.length === 0) return
        this.fileList = data.map(v => {
          return {name: v.originalName, response: {id: v.id}}
        })
      }
    )
  }
}
</script>

<style lang="less" scoped>
</style>
